package com.qipay.baas.mapper;

import com.qipay.baas.model.OperateLog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface OperateLogMapper {
    @Delete({
        "delete from operate_log",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into operate_log (user_id, service, ",
        "ip, operate_date, ",
        "url, `method`, param, ",
        "info)",
        "values (#{userId,jdbcType=INTEGER}, #{service,jdbcType=VARCHAR}, ",
        "#{ip,jdbcType=VARCHAR}, #{operateDate,jdbcType=TIMESTAMP}, ",
        "#{url,jdbcType=VARCHAR}, #{method,jdbcType=VARCHAR}, #{param,jdbcType=VARCHAR}, ",
        "#{info,jdbcType=VARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(OperateLog record);

    @InsertProvider(type=OperateLogSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insertSelective(OperateLog record);

    @Select({
        "select",
        "id, user_id, service, ip, operate_date, url, `method`, param, info",
        "from operate_log",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="service", property="service", jdbcType=JdbcType.VARCHAR),
        @Result(column="ip", property="ip", jdbcType=JdbcType.VARCHAR),
        @Result(column="operate_date", property="operateDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="method", property="method", jdbcType=JdbcType.VARCHAR),
        @Result(column="param", property="param", jdbcType=JdbcType.VARCHAR),
        @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR)
    })
    OperateLog selectByPrimaryKey(Integer id);

    @UpdateProvider(type=OperateLogSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(OperateLog record);

    @Update({
        "update operate_log",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "service = #{service,jdbcType=VARCHAR},",
          "ip = #{ip,jdbcType=VARCHAR},",
          "operate_date = #{operateDate,jdbcType=TIMESTAMP},",
          "url = #{url,jdbcType=VARCHAR},",
          "`method` = #{method,jdbcType=VARCHAR},",
          "param = #{param,jdbcType=VARCHAR},",
          "info = #{info,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(OperateLog record);
}